package com.oil.petro.eshop.service;

import com.oil.petro.eshop.bean.CartBean;
import com.oil.petro.eshop.dao.OrderDAO;
import com.oil.petro.eshop.dao.ProductDAO;
import com.oil.petro.eshop.entity.Account;
import com.oil.petro.eshop.entity.Order;
import com.oil.petro.eshop.entity.Product;
import com.oil.petro.eshop.utils.OrderStatusUtil;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional()
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderDAO orderDAO;

    @Autowired
    ProductDAO productDAO;

    @Override
    public void updateOrder(Order order) {
        orderDAO.update(order);
    }

    @Override
    public void saveOrder(Order order) {
        orderDAO.save(order);
    }

    @Transactional
    @Override
    public void addOrder(int accountId, int productId, int quantity) {

        Order order = new Order(quantity, productId, accountId);

        orderDAO.save(order);

    }

    @Transactional
    @Override
    public List<CartBean> getOrders(int accountId) {

        List<CartBean> list = new ArrayList<>();
        CartBean cartBean;
        Product product;
        Order order;
        // Integer productId;
        int counter = 1;
        for (Order o : orderDAO.getOrderedProducts(accountId)) {
            product = productDAO.get(o.getProductId());
            order = orderDAO.get(accountId, o.getProductId());
            cartBean = new CartBean(counter, order.getOrderedQuantity(), product, o.getId());
            list.add(cartBean);
            counter++;
        }
        return list;
    }

    @Transactional
    @Override
    public boolean isEmpty(int accountId, int productId) {

        return orderDAO.isEmpty(accountId, productId);

    }

    @Transactional
    @Override
    public Order getOrder(int accountId, int productId) {

        return orderDAO.get(accountId, productId);
    }

    @Transactional
    @Override
    public void removeOrder(int orderId) {

        Order order = orderDAO.get(orderId);
        Product product = productDAO.get(order.getProductId());
        product.setAmount(product.getAmount() + order.getOrderedQuantity());
        productDAO.update(product);
        orderDAO.delete(orderId);
    }

    @Transactional
    @Override
    public void removeOrders(int accountId) {
        Product product;
        for (Order o : orderDAO.getOrderedProducts(accountId)) {
            orderDAO.delete(o);
            product = productDAO.get(o.getProductId());

            if (product.getAmount() == 0) {
                product.setOrderStatus(OrderStatusUtil.UNAVAILABLE);

            }

            if (!isInCart(product.getId())) {
                product.setOrderStatus(OrderStatusUtil.NEW);
            }
            productDAO.update(product);

        }

    }

    @Transactional
    @Override
    public float getTotalCost(int accountId) {

        float sum = 0;
        for (CartBean item : getOrders(accountId)) {

            sum += item.getPriceForAll();
        }

        return sum;

    }

    private boolean isInCart(int porductId) {

        List<Order> list = orderDAO.getAccountsForProduct(porductId);

        if (list.size() > 0) {
            return true;
        } else {
            return false;
        }

    }
}
